<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 标题 -->
      <div class="title">
        <h1>学成在线统计系统</h1>
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="loginFormRef" class="login_form" :model="loginForm" :rules="loginFormRules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            placeholder="账户"
            v-model="loginForm.username"
            @focus="focushandle"
            @blur="blurhandle"
          >
            <img slot="prefix" src="@/assets/images/zhanghu@2x.png" />
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="密码"
            type="password"
            v-model="loginForm.password"
            @focus="focushandle"
            @blur="blurhandle"
          >
            <img slot="prefix" src="@/assets/images/mima@2x.png" />
          </el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="verifycode">
          <el-input
            placeholder="验证码"
            v-model="loginForm.verifycode"
            @focus="focushandle"
            @blur="blurhandle"
          >
            <img slot="prefix" src="@/assets/images/yanzhengma@2x.png" />
            <canvas slot="suffix" id="verifybox"></canvas>
          </el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="clickHandler">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Gcode from "../../utils/verify";
import { getItem } from "../../utils/storage";
import request from "../../utils/request";
export default {
  name: "Login",
  data() {
    var validateVerify = (rule, value, callback) => {
      let vCode = getItem("verify_code");
      if (value.length !== 4) {
        callback(new Error("请输入4位验证码"));
      } else if (value !== vCode) {
        callback(new Error("请输入正确的验证码"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "fsy",
        password: "000000",
        verifycode: ""
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ],
        verifycode: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { validator: validateVerify, trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    new Gcode("#verifybox");
  },
  methods: {
    async clickHandler() {
      // 预校验
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return this.$message({
            showClose: true,
            message: "请输入正确内容",
            type: "error"
          });
        }

        const { data: res } = await request.post(
          "/common/login",
          this.loginForm
        );
        if (res.code !== 200) {
          return this.$message({
            showClose: true,
            message: "登录失败",
            type: "error"
          });
        }
        this.$router.push("/home");
      });
    },
    focushandle(event) {
      event.target.nextElementSibling.children[0].style.cssText =
        "border:1px dashed #ccc;";
    },
    blurhandle(event) {
      event.target.nextElementSibling.children[0].style.border = 0;
    }
  }
};
</script>

<style lang="less" scoped>
.login_container {
  background-image: url("../../assets/images/background.png");
  background-size: cover;
  height: 100%;
  overflow: hidden;
  .login_box {
    width: 650px;
    height: 600px;
    background: rgba(255, 255, 255, 0.89);
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .title {
      width: 288px;
      margin: 87px auto;
      h1 {
        font-size: 36px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: rgba(27, 123, 239, 1);
        line-height: 50px;
      }
    }
    .login_form {
      width: 384px;
      margin: 0 auto;
      img {
        height: 30px;
      }
      /deep/.el-input__inner {
        background-color: transparent;
        padding-left: 52px;
        border: 0;
        border-radius: 0;
        border-bottom: 1px solid #cdcdcd;
      }
      .btns {
        margin-top: 70px;
        .el-button {
          width: 382px;
          height: 60px;
          border: 1px solid #007bff;
          background: linear-gradient(
            270deg,
            rgba(94, 252, 232, 1) 0%,
            rgba(115, 110, 254, 1) 100%
          );
          border-radius: 6px;
        }
      }
    }
  }
}
</style>
